from pathlib import Path
from tkinter import *
from gui.main_window import revamp
from gui.main_window import view_mode
# import view_mode  # 此为本地调试模式
# from .. import login
# ..是相对路径
OUTPUT_PATH = Path(__file__).parent
ASSETS_PATH = OUTPUT_PATH / Path("./img")

def relative_to_assets(path: str) -> Path:
    return ASSETS_PATH / Path(path)

def main_Window(user = None,passwd=None):
    MainWindow(user,passwd)


class MainWindow():
    def __init__(self,user = None,passwd=None):
        self.user_1 = user
        self.passwd_1 = passwd
        self.root= Tk()
        self.root.title(f"HIK WMS 1.0----{user}")
        # root.config(bg = "#FFFFFF")
        self.root_height = 540
        self.root_width = 800
        sx = self.root.winfo_screenwidth()
        sy = self.root.winfo_screenheight()
        cw_x = (sx - self.root_width) / 2
        cw_y = (sy - self.root_height) / 2
        self.root.geometry("%dx%d+%d+%d" % (self.root_width, self.root_height, cw_x, cw_y))

        # 全局变量定义区
        self.home_a = 0
        self.home_open = 0
        self.view_a = 0
        self.view_open = 0

        self.mess_a = 0
        bg = "#47484c"

        # log 图标存储区
        # 左下角log图区

        photo0 = PhotoImage(file=relative_to_assets("photo0.png"))
        photo1 = PhotoImage(file=relative_to_assets("photo1.png"))
        photo2 = PhotoImage(file=relative_to_assets("photo2.png"))
        photo3 = PhotoImage(file=relative_to_assets("photo3.png"))
        photo4 = PhotoImage(file=relative_to_assets("photo4.png"))
        photo5 = PhotoImage(file=relative_to_assets("photo5.png"))


        self.photo6 = PhotoImage(file=relative_to_assets("photo6.png"))
        self.photo7 = PhotoImage(file=relative_to_assets("photo7.png"))
        self.photo8 = PhotoImage(file=relative_to_assets("photo8.png"))
        self.photo9 = PhotoImage(file=relative_to_assets("photo9.png"))
        self.photo10 = PhotoImage(file=relative_to_assets("photo10.png"))
        self.photo11 = PhotoImage(file=relative_to_assets("photo11.png"))
        self.photo12 = PhotoImage(file=relative_to_assets("photo12.png"))
        self.photo13 = PhotoImage(file=relative_to_assets("photo13.png"))
        self.photo14 = PhotoImage(file=relative_to_assets("photo14.png"))
        self.photo15 = PhotoImage(file=relative_to_assets("photo15.png"))
        self.photo16 = PhotoImage(file=relative_to_assets("photo16.png"))
        self.photo17 = PhotoImage(file=relative_to_assets("photo17.png"))
        self.photo18 = PhotoImage(file=relative_to_assets("photo18.png"))
        self.photo19 = PhotoImage(file=relative_to_assets("photo19.png"))
        self.photo20 = PhotoImage(file=relative_to_assets("photo20.png"))
        self.photo21 = PhotoImage(file=relative_to_assets("photo21.png"))
        self.photo22 = PhotoImage(file=relative_to_assets("photo22.png"))
        self.photo23 = PhotoImage(file=relative_to_assets("photo23.png"))
        self.photo24 = PhotoImage(file=relative_to_assets("photo24.png"))


        # 菜单
        main_Menu = Menu(self.root)
        main_Menu.add_command(label = "文件(F)")
        main_Menu.add_command(label = "功能(F1)",command=lambda: self.home(self.all_cvas, self.home_cvas),)
        main_Menu.add_command(label = "视图(V)",command=lambda: self.home(self.all_cvas, self.view_cvas),)
        main_Menu.add_command(label = "关闭视图(V)",command=lambda: self.close(self.all_cvas))
        self.root.config(menu = main_Menu)




        # 绘画布图区

        # 顶部功能和视图布图区
        self.canvas_hometop = Canvas(
            self.root,
            bg="white",
            height=0,
            width=180,
            bd=0,
            highlightthickness=0,
            relief="ridge",
        )
        self.canvas_hometop.pack(side=TOP, fill=X)






        # 左边布局
        frame= Frame(
            self.root,
            bg="white",
            height=520,
            width=180,
            bd=0,
            highlightthickness=0,
            relief="ridge",

        )
        frame.pack(side = LEFT,fill = Y)







        # 左下角布局，主分类按钮

        canvas_leftbottom0 = Canvas(
            frame,
            bg=bg,
            height=240,
            width=180,
            bd=0,
            highlightthickness=0,
            relief="ridge",
        )
        canvas_leftbottom0.pack(side=BOTTOM, fill=X)




        # 左上角滑动条底座
        frame0 = Frame(
            frame,
            bg="white",
            height=255,
            width=200,


        )
        frame0.pack(side=LEFT, fill=Y)


        # 调用view_mode按钮嵌入式滑动条模块
        self.scr = view_mode.ScrollableFrame(frame0)
        self.scr.pack(fill=BOTH, expand=1)




        canvas_leftbottom = Canvas(
            canvas_leftbottom0,
            bg= bg,
            height=400,
            width=180,
            bd=0,
            highlightthickness=0,
            relief="ridge",
        )
        canvas_leftbottom.pack(side = LEFT,fill = Y)

        #  滚动条区





        # 左下角按钮
        im_button = Button(canvas_leftbottom, image=photo5, compound=LEFT, activebackground="#A1A1A1",
                       command = lambda :self.handle_btn_press(self.all_but,self.im_but),text="信息管理",relief="ridge", bd=0, fg="#FFFFFF", bg=bg, width=180)
        im_button.grid(row=0, column=0, padx=2, pady=3)
        commod_button = Button(canvas_leftbottom,image = photo0,compound = LEFT,cursor = "star",activebackground = "#A1A1A1",
                        command = lambda :self.handle_btn_press(self.all_but,self.commod_but),text = "商品管理",relief = "ridge",bd = 0,fg ="#FFFFFF",bg = bg,width = 180)
        commod_button.grid(row = 1, column = 0,padx = 2,pady = 3)
        it_button = Button(canvas_leftbottom,image = photo1,compound = LEFT,cursor = "star",activebackground = "#A1A1A1",
                        command = lambda :self.handle_btn_press(self.all_but,self.it_but),text = "库存管理",relief = "ridge",bd = 0,fg ="#FFFFFF",bg = bg,width = 180)
        it_button.grid(row = 2, column = 0,padx = 2,pady = 3)
        report_botton = Button(canvas_leftbottom,image = photo2,compound = LEFT,cursor = "star",activebackground = "#A1A1A1",
                        command=lambda: self.handle_btn_press(self.all_but, self.report_but),text = "报表管理",relief = "ridge",bd = 0,fg ="#FFFFFF",bg = bg,width = 180)
        report_botton.grid(row = 3, column = 0,padx = 2,pady = 3)
        purchase_button = Button(canvas_leftbottom,image = photo3,compound = LEFT,cursor = "star",activebackground = "#A1A1A1",
                        command=lambda: self.handle_btn_press(self.all_but, self.purchase_but),text = "采购管理",relief = "ridge",bd = 0,fg ="#FFFFFF",bg = bg,width = 180)
        purchase_button.grid(row = 4, column = 0,padx = 2,pady = 3)
        qui_button = Button(canvas_leftbottom, image=photo4, compound=LEFT, activebackground="#A1A1A1",
                        command=lambda: self.handle_btn_press(self.all_but, self.qui_but),text="退       出",relief="ridge", bd=0, fg="#FFFFFF", bg=bg, width=180)
        qui_button.grid(row=5, column=0, padx=2, pady=3)


        # 最开始的子Button，信息管理
        self.myz_button = Button(self.scr.scrollable_frame, image=self.photo6, compound=LEFT,
                            activebackground="#A1A1A1",
                            text="个人中心", relief="ridge", bd=0, fg="#515151", anchor=W, bg="white", width=180)
        self.myz_button.pack(side=TOP, fill=X, pady=5)

        self.password_button = Button(self.scr.scrollable_frame, image=self.photo7, compound=LEFT,
                                 activebackground="#A1A1A1", anchor=W,command = self.revamp,
                                 text="修改密码", relief="ridge", bd=0, fg="#515151", bg="white", width=180)
        self.password_button.pack(side=TOP, fill=X, pady=5)



        # 商品管理
        self.store_button = Button(self.scr.scrollable_frame, image=self.photo14, compound=LEFT,
                                   activebackground="#A1A1A1",
                                   text="仓库区域", relief="ridge", bd=0, fg="#515151", anchor=W, bg="white", width=180)

        self.goods_button = Button(self.scr.scrollable_frame, image=self.photo15, compound=LEFT,
                                   activebackground="#A1A1A1",
                                   text="货架定义", relief="ridge", bd=0, fg="#515151", anchor=W, bg="white", width=180)
        self.class_button = Button(self.scr.scrollable_frame, image=self.photo8, compound=LEFT,
                            activebackground="#A1A1A1",
                            text="商品分类", relief="ridge", bd=0, fg="#515151", anchor=W, bg="white", width=180)
        # self.class_button.pack(side=TOP, fill=X, pady=5)



        # 库存管理
        self.clas_button = Button(self.scr.scrollable_frame, image=self.photo9, compound=LEFT,
                              activebackground="#A1A1A1",
                              text="库存查询", relief="ridge", bd=0, fg="#515151", anchor=W, bg="white", width=180)
        # clas_button.pack(side=TOP, fill=X, pady=5)

        self.int_button = Button(self.scr.scrollable_frame, image=self.photo10, compound=LEFT,
                              activebackground="#A1A1A1",
                              text="入库管理", relief="ridge", bd=0, fg="#515151", anchor=W, bg="white", width=180)
        # int_button.pack(side=TOP, fill=X, pady=5)

        self.out_button = Button(self.scr.scrollable_frame, image=self.photo11, compound=LEFT,
                            activebackground="#A1A1A1",
                            text="出库管理", relief="ridge", bd=0, fg="#515151", anchor=W, bg="white", width=180)
        # out_button.pack(side=TOP, fill=X, pady=5)

        self.check_button = Button(self.scr.scrollable_frame, image=self.photo12, compound=LEFT,
                            activebackground="#A1A1A1",
                            text="库存盘点", relief="ridge", bd=0, fg="#515151", anchor=W, bg="white", width=180)
        # check_button.pack(side=TOP, fill=X, pady=5)

        self.early_button = Button(self.scr.scrollable_frame, image=self.photo13, compound=LEFT,
                            activebackground="#A1A1A1",
                            text="库存预警", relief="ridge", bd=0, fg="#515151", anchor=W, bg="white", width=180)
        # early_button.pack(side=TOP, fill=X, pady=5)

        # 报表管理
        self.bb_button = Button(self.scr.scrollable_frame, image=self.photo16, compound=LEFT,
                                   activebackground="#A1A1A1",
                                   text="库存报表", relief="ridge", bd=0, fg="#515151", anchor=W, bg="white", width=180)

        self.outb_button = Button(self.scr.scrollable_frame, image=self.photo17, compound=LEFT,
                                   activebackground="#A1A1A1",
                                   text="出库报表", relief="ridge", bd=0, fg="#515151", anchor=W, bg="white", width=180)
        self.intb_button = Button(self.scr.scrollable_frame, image=self.photo18, compound=LEFT,
                                   activebackground="#A1A1A1",
                                   text="入库报表", relief="ridge", bd=0, fg="#515151", anchor=W, bg="white", width=180)

        # 采购管理
        self.cai_button = Button(self.scr.scrollable_frame, image=self.photo21, compound=LEFT,
                                  activebackground="#A1A1A1",
                                  text="采购订单", relief="ridge", bd=0, fg="#515151", anchor=W, bg="white", width=180)
        self.gong_button = Button(self.scr.scrollable_frame, image=self.photo22, compound=LEFT,
                                  activebackground="#A1A1A1",
                                  text="供应管理", relief="ridge", bd=0, fg="#515151", anchor=W, bg="white", width=180)

        # 退出
        self.anew_button = Button(self.scr.scrollable_frame, image=self.photo19, compound=LEFT,
                                  activebackground="#A1A1A1",command = self.logout,
                                  text="重新登录", relief="ridge", bd=0, fg="#515151", anchor=W, bg="white", width=180)
        self.quii_button = Button(self.scr.scrollable_frame, image=self.photo20, compound=LEFT,
                                  activebackground="#A1A1A1",command = lambda : self.root.destroy(),
                                  text="退出程序", relief="ridge",bd=0, fg="#515151", anchor=W, bg="white", width=180)

        # home功能框
        self.canvas_home = Canvas(self.canvas_hometop,bg="white",height=70,width=180,bd=0,highlightthickness=0,
                                  relief="ridge",)
        Button(self.canvas_home, text="Refresh",image = self.photo24,compound=TOP,relief="ridge",bd=0,bg="white").pack(side=LEFT, fill=Y)

        self.canvas_view = Canvas(self.canvas_hometop,bg="red",height=70,width=180,bd=0,highlightthickness=0,
                                  relief="ridge",)


        # 子Button厉遍
        self.all_but = [self.myz_button, self.password_button, self.class_button,self.store_button,self.goods_button, self.clas_button, self.int_button,
                        self.out_button, self.check_button, self.early_button,self.bb_button, self.outb_button, self.cai_button,self.gong_button,
                        self.intb_button,self.anew_button,self.quii_button]
        self.im_but = [self.myz_button, self.password_button]
        self.commod_but = [self.class_button,self.store_button,self.goods_button]
        self.it_but = [self.clas_button, self.int_button, self.out_button, self.check_button, self.early_button]
        self.report_but = [self.bb_button, self.outb_button, self.intb_button]
        self.purchase_but = [self.cai_button,self.gong_button]
        self.qui_but = [self.anew_button,self.quii_button]

        # home功能框厉遍
        self.all_cvas = [self.canvas_home,self.canvas_view]
        self.home_cvas = [self.canvas_home]
        self.view_cvas = [self.canvas_view]



        #
        self.root.mainloop()




    # 子 Button厉遍函数
    def handle_btn_press(self, but,name ):
        for i_but in but:
            i_but.pack_forget()

        for a_but in name:

            a_but.pack(side=TOP, fill=X, pady=5)


    def home(self,cvas,name):
        for i_cvas in cvas:
            i_cvas.pack_forget()

        for a_cvas in name:
            a_cvas.config(height=70)
            a_cvas.pack(side=TOP, fill=X)


    def close(self,cvas):
        for i_cvas in cvas:
            i_cvas.pack_forget()
            i_cvas.config(height=0)



    def logout(self):

            self.root.destroy()
            login.gui.Login()

    def revamp(self):
        revamp.Revamp(self.root,self.photo23,self.user_1,passwd=self.passwd_1,logout=self.logout)



if __name__ == "__main__":
    main_Window("肖丰")